<template>
  <div class="app-container">

    <template>
      <el-table :data="contextList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="供应商名称" prop="vendorName" align="center" />
        <el-table-column label="联系人" prop="vendorContact" align="center" />
        <el-table-column label="联系电话" prop="vendorPhone" align="center" />
        <!-- 嵌套表格展示采购订单 -->
        <el-table-column label="采购订单" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="showPurchaseOrderDetails(scope.row)">查看采购订单详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 对话框 -->
      <el-dialog title="采购订单详情" :visible.sync="dialogVisible" width="60%">
        <el-table :data="currentPurchaseOrdersList" style="width: 100%">
          <el-table-column label="采购单号" prop="puoNum" align="center" />
          <el-table-column label="标题" prop="puoTitle" align="center" />
          <el-table-column label="采购日期" prop="puoTime" align="center" />
          <el-table-column label="采购总数" align="center" prop="puoTololenum" />
          <el-table-column label="实收总数" align="center" prop="puoActualTotalnum" />
          <el-table-column label="验收人" align="center" prop="puoAcceptor" />
          <el-table-column label="验收日期" align="center" prop="puoAcceptanceTime" />
        </el-table>
        <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">关闭</el-button>
    </span>
      </el-dialog>
    </template>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-divider content-position="center">入库管理信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPurchaseOrders">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePurchaseOrders">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="purchaseOrdersList" :row-class-name="rowPurchaseOrdersIndex" @selection-change="handlePurchaseOrdersSelectionChange" ref="purchaseOrders">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="采购单号" prop="puoNum" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.puoNum" placeholder="请选择采购单号">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="标题" prop="puoTitle" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.puoTitle" placeholder="请输入标题" />
            </template>
          </el-table-column>
          <el-table-column label="采购总数" prop="puoTololenum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.puoTololenum" placeholder="请输入采购总数" />
            </template>
          </el-table-column>
          <el-table-column label="实收总数" prop="puoActualTotalnum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.puoActualTotalnum" placeholder="请输入实收总数" />
            </template>
          </el-table-column>
          <el-table-column label="验收人" prop="puoAcceptor" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.puoAcceptor" placeholder="请输入验收人" />
            </template>
          </el-table-column>
          <el-table-column label="验收日期" prop="puoAcceptanceTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.puoAcceptanceTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择验收日期" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listContext, getContext, delContext, addContext, updateContext } from "@/api/context/context";

export default {
  name: "Context",
  data() {
    return {
      // 当前显示的采购订单列表
      currentPurchaseOrdersList: [],
      // 控制对话框的显示状态
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPurchaseOrders: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商详情表格数据
      contextList: [],
      // 入库管理表格数据
      purchaseOrdersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供应商详情列表 */
    getList() {
      this.loading = true;
      listContext(this.queryParams).then(response => {
        this.contextList = response.rows;
        this.total = response.total;
        console.log(this.contextList);
        this.purchaseOrdersList = this.contextList[0].purchaseOrdersList;
        console.log(this.purchaseOrdersList);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        vendorName: null,
        vendorContact: null,
        vendorAddress: null,
        vendorPhone: null,
        vendorIllustrate: null,
        vendorCert: null,
        vaId: null,
        addTime: null,
        vendorScore: null
      };
      this.purchaseOrdersList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.vaId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const vaId = row.vaId || this.ids
      getContext(vaId).then(response => {
        this.form = response.data;
        this.purchaseOrdersList = response.data.purchaseOrdersList;
        this.open = true;
        this.title = "修改供应商详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.purchaseOrdersList = this.purchaseOrdersList;
          if (this.form.vaId != null) {
            updateContext(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContext(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vaIds = row.vaId || this.ids;
      this.$modal.confirm('是否确认删除供应商详情编号为"' + vaIds + '"的数据项？').then(function () {
        return delContext(vaIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 入库管理序号 */
    rowPurchaseOrdersIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 入库管理添加按钮操作 */
    handleAddPurchaseOrders() {
      let obj = {};
      obj.puoNum = "";
      obj.puoTitle = "";
      obj.puoTololenum = "";
      obj.puoActualTotalnum = "";
      obj.puoAcceptor = "";
      obj.puoAcceptanceTime = "";
      this.purchaseOrdersList.push(obj);
    },
    /** 入库管理删除按钮操作 */
    handleDeletePurchaseOrders() {
      if (this.checkedPurchaseOrders.length == 0) {
        this.$modal.msgError("请先选择要删除的入库管理数据");
      } else {
        const purchaseOrdersList = this.purchaseOrdersList;
        const checkedPurchaseOrders = this.checkedPurchaseOrders;
        this.purchaseOrdersList = purchaseOrdersList.filter(function (item) {
          return checkedPurchaseOrders.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handlePurchaseOrdersSelectionChange(selection) {
      this.checkedPurchaseOrders = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('context/context/export', {
        ...this.queryParams
      }, `context_${new Date().getTime()}.xlsx`)
    },
    async fetchSuppliersData() {
      // 假设这是从后端获取供应商数据的方法
      const response = await axios.get('/api/suppliers');
      this.contextList = response.data;
    },
    showPurchaseOrderDetails(row) {
      // 显示采购订单详情对话框
      this.currentPurchaseOrdersList = row.purchaseOrdersList || [];
      this.dialogVisible = true;
    }
  },

};
</script>
<style scoped>/* 添加一些自定义样式 */
.el-dialog__body {
  padding: 20px;
}
</style>
